package com.example.annotation_compiler;

import com.example.annotation.BindPath;
import com.google.auto.service.AutoService;

import java.io.IOException;
import java.io.Writer;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.Filer;
import javax.annotation.processing.ProcessingEnvironment;
import javax.annotation.processing.Processor;
import javax.annotation.processing.RoundEnvironment;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;

//javac  就知道你
@AutoService(Processor.class)
public class AnnotationCompiler extends AbstractProcessor {

//    准备工作

    //生成代码的工具
    Filer filer;
    @Override
    public synchronized void init(ProcessingEnvironment processingEnv) {
        super.init(processingEnv);
        filer = processingEnv.getFiler();
    }
//    处理什么组件


    @Override
    public Set<String> getSupportedAnnotationTypes() {
        Set<String> types = new HashSet<>();
        types.add(BindPath.class.getCanonicalName());
        return types;
    }
//jdk版本
    @Override
    public SourceVersion getSupportedSourceVersion() {
        return processingEnv.getSourceVersion();
    }


    @Override
    public boolean process(Set<? extends TypeElement> set, RoundEnvironment roundEnvironment) {
//           1         elemtn
//        MerberActivity   PersonActivity 是   elements
        Set<? extends Element> elements =roundEnvironment.getElementsAnnotatedWith(BindPath.class);
//        零部件      MerberActivity  长城汽车 的   所有零部件    PersonActivity 比亚迪 的   所有零部件
        Map<String,String> map = new HashMap<>();
        for (Element element : elements) {
//写文件
            TypeElement typeElement = (TypeElement) element;
            String activityName = typeElement.getQualifiedName().toString();
//            key    meber/PersonActivity.class
            String key = typeElement.getAnnotation(BindPath.class).value();
            map.put(key,activityName+".class");
        }
        if(map.size() >0){
//写文件
            //生成工具类  然后写代码
            //工具类的类名
            String className = "ActivityUtil"+System.currentTimeMillis();
            Writer writer = null;
            try {

                writer= filer.createSourceFile("com.example.util." + className).openWriter();
                StringBuffer stringBuffer = new StringBuffer();
                stringBuffer.append("package com.example.util;\n");

                stringBuffer.append("import com.example.arouter.ARouter;\n");
                stringBuffer.append("import com.example.arouter.IRouter;\n");
                stringBuffer.append("public class " + className + " implements IRouter {\n");
                stringBuffer.append("@Override\n");
                stringBuffer.append("public void putActivity() {\n");
                Iterator<String> iterator = map.keySet().iterator();
                while (iterator.hasNext()){
                    String key = iterator.next();
                    String activityName = map.get(key);
                    stringBuffer.append("ARouter.getInstance().addActivity(\""+key+"\","+activityName+");");

                }
                stringBuffer.append("\n}\n}");
                writer.write(stringBuffer.toString());

            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                if(writer!=null){
                    try {
                        writer.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return false;
    }
}
